<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Veh_Fuel_List extends Page
{
       public $lblmodel = null;
       public $lblmake = null;
       public $Label43 = null;
       public $Label38 = null;
       public $lblyear = null;
       public $lblplate = null;
       public $lblEmpName = null;
       public $Label18 = null;
       public $lblCurrOd = null;
       public $Label16 = null;
       public $Label14 = null;
       public $JTGroupBox1 = null;
       public $btnSearch = null;
       public $edtSearch = null;
       public $Image4 = null;
       public $lblname = null;
       public $lblCurrOdo = null;
       public $lblowner = null;
       public $lblownership = null;
       public $lblEng = null;
       public $btnExport = null;
       public $lblVIN = null;
       public $Label12 = null;
       public $lblDriver = null;
       public $Label10 = null;
       public $Label7 = null;
       public $JTFuelHidden = null;
       public $lblDept = null;
       public $lblmobile = null;
       public $lblphone = null;
       public $lblemail = null;
       public $lblmname = null;
       public $lblfname = null;
       public $lblempid = null;
       public $JTJavaScript2 = null;
       public $Label21 = null;
       public $Label20 = null;
       public $JTDivDriver = null;
       public $Label17 = null;
       public $Label15 = null;
       public $Label13 = null;
       public $Label11 = null;
       public $Label9 = null;
       public $Label5 = null;
       public $Label3 = null;
       public $Image2 = null;
       public $lbltype = null;
       public $lblvehmodel = null;
       public $lblvehmake = null;
       public $lblvehyear = null;
       public $Label8 = null;
       public $Label6 = null;
       public $Label4 = null;
       public $Label2 = null;
       public $Label1 = null;
       public $JTDivVeh = null;
       public $JTJavaScript1 = null;
       public $imgBack = null;
       public $Image1 = null;
       public $imgAddFuel = null;
       public $hiddenId = null;
       public $JTSiteTheme1 = null;
       public $JTPlatinumGrid1 = null;

       function btnExportJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/excel.png"
       <?php

       }

       function btnExportJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/excel2.png"
       <?php

       }

       function btnExportClick($sender, $params)
       {
       $this->JTFuelHidden->exportGridToXLSDownload('fuel_list.xls');


       }

       function JTFuelHiddenShow($sender, $params)
       {
       $veh_id = $this->hiddenId->getValue();

       $query1 = "select * from vehicle_tbl where vehicle_id='".$this->hiddenId->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());
       $row = mysql_fetch_array($result);
       $label_od = $row['track_by'];


       if ($label_od=='distance')
       {
       $string1 = 'Prev Odometer';
       $string2 = 'Curr Odometer';
       }
       else
       {
       $string1 = 'Last Clock Hrs';
       $string2 = 'Clock Hrs Now';
       }

       $query1 = "select * from veh_fuel_tbl where vehicle_id='".$veh_id."' and company_id='".$_SESSION['company_id']."' and
       (date like '%".$this->edtSearch->Text."%'
       or brand like '%".$this->edtSearch->Text."%'
       or engine like '%".$this->edtSearch->Text."%') order by date desc";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();



       $arr[] = array(
                        '1' => 'Plate Num:'.' '.$row['veh_num'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' '
                    );

       $arr[] = array(
                        '1' => 'Year:'.' '.$row['year'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' '
                    );

       $arr[] = array(
                        '1' => 'Make:'.' '.$row['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' '
                    );

       $arr[] = array(
                        '1' => 'Model:'.' '.$row['model_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' '
                    );

       $arr[] = array(
                        '1' => 'Type/Style:'.' '.$row['style_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' '
                    );

       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' '
                    );


       $arr[] = array(
                        '1' => 'FUEL REPORT',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' '
                    );

      $arr[] = array(
      '1' => 'Date',
      '2'=> 'Driver',
      '3' => 'Brand',
      '4' => 'Type',
      '5' => 'Liters',
      '6' => 'Price/Li',
      '7' => 'Total',
      '8' => 'Km/Hrs',
      '9' => 'Km/L'
      );

      while($row = mysql_fetch_array($result))
      {
         $ider = $row['driver_id'];
         $result_driver = mysql_query("SELECT * FROM employee_tbl WHERE autoid='".$ider."'");
         $row_driver = mysql_fetch_array($result_driver);

         $track_by = $row['track_by'];
         if($track_by=='distance')
         {
         $lbl = 'Km';
         }
         if($track_by=='hours')
         {
         $lbl = 'Hrs';
         }

         $empid = $row_driver['emp_id'];
         $lname = $row_driver['lname'];
         $fname = $row_driver['fname'];
         $mname = $row_driver['mname'];
         $email = $row_driver['email'];
         $phone = $row_driver['phone_num'];
         $mobile = $row_driver['mobile_num'];
         $Dept = $row_driver['department'];

         $kml = $row['miles']/$row['gallons'];
         //$mname = "a";
         //$email = "a";
         //$phone = "a";
         //$mobile = "a";
         //$Dept = "a";

         $date = "<a href=veh_fuel_edit.php?veh=".$veh_id."&fuel=".$row['fuel_id']." title='Edit Fuel Record'>".$row['date']."</a>";
         $tools = "<img onclick=js_deleteFuel('".$veh_id."','".$row['fuel_id']."'); src='image/delete_orange.jpg' title='delete'>";
         //"<a href=veh_fuel_delete.php?veh=".$veh_id."&fuel=".$row['fuel_id']."><img src='image/delete_orange.jpg' title='delete' no border></a>";
         $driver = "<a href='#' onClick=showDriInfo('".$empid."','".urlencode($lname)."','".urlencode($fname)."','".urlencode($mname)."','".$email."','".$phone."','".$mobile."','".urlencode($Dept)."') title='Driver Information'>".$row['driver']."</a>";
         //"<img onclick=showDriInfo('".$empid."','".urlencode($lname)."','".urlencode($fname)."','".urlencode($mname)."','".$email."','".$phone."','".$mobile."','".urlencode($Dept)."'); src='image/orange_information.jpg' title='Driver Information'>";

         $arr[] = array(
                        '1' => $row['date'],
                        '2'=>$row['driver'],
                        '3' => $row['brand'],
                        '4' => $row['type'],
                        '5' => $row['gallons'],
                        '6' => $row['price'],
                        '7' => $row['tot_cost'],
                        '8' => $row['miles'].$lbl,
                        '9' => $kml
                        );


      }
      $total_miles = mysql_query("SELECT SUM(miles) as sum_miles FROM veh_fuel_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_miles = mysql_fetch_array($total_miles);

      $total_gallons = mysql_query("SELECT SUM(gallons) as sum_gallons FROM veh_fuel_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_gallons = mysql_fetch_array($total_gallons);

      $total_cost = mysql_query("SELECT SUM(tot_cost) as sum_cost FROM veh_fuel_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_cost = mysql_fetch_array($total_cost);


      $arr[] = array(
                        '1' => TOTAL,
                        '5' => $row_gallons['sum_gallons'],
                        '7' => $row_cost['sum_cost'],
                        '8' => $row_miles['sum_miles']
                        );



          $this->JTFuelHidden->setCellData($arr);


       }

       function Image2JSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById( "JTDivVeh" ).Show();
       <?php

       }

       function Image2Click($sender, $params)
       {
       $this->JTDivVeh->StartVisible=true;
        redirect('veh_fuel_list.php?veh='.$this->hiddenId->getValue());


       }

       function lblVehClick($sender, $params)
       {
        $this->JTDivVeh->StartVisible=true;
        redirect('veh_fuel_list.php?veh='.$this->hiddenId->getValue());


       }

       function btnSearchClick($sender, $params)
       {
       redirect('veh_fuel_list.php?veh='.$this->hiddenId->getValue());
       }





       function edtSearchJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edtSearch').value = '';
       <?php

       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function imgAddFuelJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgAddFuel'].src="image/add_new.png"
       <?php

       }

       function imgAddFuelJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgAddFuel'].src="image/add_new_blue.png"
       <?php

       }



       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }


       function Veh_Fuel_ListCreate($sender, $params)
       {

       if($_SESSION['access_vehicle']==1)
       {
        $this->imgAddFuel->setVisible(false);
        $this->btnExport->Left = 943;
       }

       if($_SESSION['access_vehicle']==2)
       {
        $this->imgAddFuel->setVisible(true);
        $this->btnExport->Left = 839;
       }

       $id = $_GET['veh'];
       $this->hiddenId->setValue($id);

       $this->JTDivVeh->Hide();
       $this->JTDivVeh->Left='500';

       $result_vin = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='".$this->hiddenId->getValue()."'");
       $row_selected = mysql_fetch_array($result_vin);
       $label = $row_selected['VIN'];
       //$this->lblVeh->Caption = $label;
       $this->lblyear->Caption = $row_selected['year'];
       //$this->lblvehyear->Caption = $row_selected['year'];
       $this->lblmake->Caption = $row_selected['manufacturer_name'];
       //$this->lblvehmake->Caption = $row_selected['manufacturer_name'];
       $this->lblmodel->Caption = $row_selected['model_name'];
       //$this->lblvehmodel->Caption = $row_selected['model_name'];
       //$this->lbltype->Caption = $row_selected['style_name'];
       $this->lblownership->Caption = $row_selected['ownership'];
       $this->lblowner->Caption = $row_selected['customer'];
       //$this->lblengine->Caption = $row_selected['engine_model'];
       $this->lblCurrOd->Caption = $row_selected['curr_tracking_value'];
       $this->lblDriver->Caption = $row_selected['operator'];
       $this->lblVIN->Caption = $row_selected['VIN'];
       $this->lblplate->Caption = $row_selected['veh_num'];
       $this->lblEng->Caption = $row_selected['engine_model'];

       $result_emp = mysql_query("select * from veh_fuel_tbl where vehicle_id='".$veh_id."' and company_id='".$_SESSION['company_id']."'");
       $row_emp = mysql_fetch_array($result_emp);
       $emp_id = $row_emp['driver_id'];


       /*$result_driver = mysql_query("SELECT * FROM employee_tbl WHERE company_id='".$_SESSION['company_id']."' and autoid='".$emp_id."'");
       $row_driver = mysql_fetch_array($result_driver);

       $this->lblempid->Caption = $row_driver['emp_id'];
       $this->lbllname->Caption = $row_driver['lname'];
       $this->lblfname->Caption = $row_driver['fname'];
       $this->lblmname->Caption = $row_driver['mname'];
       $this->lblemail->Caption = $row_driver['email'];
       $this->lblphone->Caption = $row_driver['phone_num'];
       $this->lblmobile->Caption = $row_driver['mobile_num'];
       $this->lblDept->Caption = $row_driver['department'];*/

       $this->imgBack->setLink('vehicle_list.php?company='.$_SESSION['company_id']);
       $this->imgAddFuel->setLink('veh_fuel_create.php?veh='.$this->hiddenId->getValue());
       }


       function JTPlatinumGrid1Show($sender, $params)
       {
       $veh_id = $this->hiddenId->getValue();

       $query1 = "select * from vehicle_tbl where vehicle_id='".$this->hiddenId->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());
       $row = mysql_fetch_array($result);
       $label_od = $row['track_by'];


       if ($label_od=='distance')
       {
       $string1 = 'Prev Odometer';
       $string2 = 'Curr Odometer';
       }
       else
       {
       $string1 = 'Last Clock Hrs';
       $string2 = 'Clock Hrs Now';
       }

       $query1 = "select * from veh_fuel_tbl where vehicle_id='".$veh_id."' and company_id='".$_SESSION['company_id']."' and
       (date like '%".$this->edtSearch->Text."%'
       or brand like '%".$this->edtSearch->Text."%'
       or driver like '%".$this->edtSearch->Text."%'
       or type like '%".$this->edtSearch->Text."%'
       or gallons like '%".$this->edtSearch->Text."%'
       or price like '%".$this->edtSearch->Text."%'
       or tot_cost like '%".$this->edtSearch->Text."%'
       or miles like '%".$this->edtSearch->Text."%')order by date DESC, fuel_id DESC";
       $result = mysql_query($query1)or die(mysql_error());

      $arr = array();
      while($row = mysql_fetch_array($result))
      {
         $ider = $row['driver_id'];
         $result_driver = mysql_query("SELECT * FROM employee_tbl WHERE autoid='".$ider."'");
         $row_driver = mysql_fetch_array($result_driver);

         $empid = $row_driver['emp_id'];
         $lname = $row_driver['lname'];
         $fname = $row_driver['fname'];
         $mname = $row_driver['mname'];
         $email = $row_driver['email'];
         $phone = $row_driver['phone_num'];
         $mobile = $row_driver['mobile_num'];
         $Dept = $row_driver['department'];

         //$mname = "a";
         //$email = "a";
         //$phone = "a";
         //$mobile = "a";
         //$Dept = "a";

         $date = "<a href=veh_fuel_edit.php?veh=".$veh_id."&fuel=".$row['fuel_id']." title='Edit Fuel'>".$row['date']."</a>";
         $tools = "<img onclick=js_deleteFuel('".$veh_id."','".$row['fuel_id']."'); src='image/delete.png' title='delete' style='cursor:pointer'>";
         //"<a href=veh_fuel_delete.php?veh=".$veh_id."&fuel=".$row['fuel_id']."><img src='image/delete_orange.jpg' title='delete' no border></a>";
         $driver = "<a href='#' onClick=showDriInfo('".$empid."','".urlencode($lname)."','".urlencode($fname)."','".urlencode($mname)."','".$email."','".$phone."','".$mobile."','".urlencode($Dept)."') title='Driver Information'>".$row['driver']."</a>";
         //"<img onclick=showDriInfo('".$empid."','".urlencode($lname)."','".urlencode($fname)."','".urlencode($mname)."','".$email."','".$phone."','".$mobile."','".urlencode($Dept)."'); src='image/orange_information.jpg' title='Driver Information'>";

         $price = $row['price'];
         $tot = $row['tot_cost'];
         $tot_cost = $this->commafy($tot);
         $pr_li = $this->commafy($price);

         $kml = $row['miles']/$row['gallons'];

         $track = $row['track_by'];
         if($track=='distance')
         {
         $mil = $row['miles'].''.'Km';
         }

         if($track=='hours')
         {
         $mil = $row['miles'].''.'Hrs';
         }

         if($_SESSION['access_vehicle']==1)
         {
         $arr[] = array(
                        'Date' => $date,
                        'Driver'=>$driver,
                        'Brand' => $row['brand'],
                        'Type' => $row['type'],
                        //'Engine Type' => $row['engine'],
                        //'Account' => $row['account'],
                        //$string1 => $row['last_odometer'],
                        //$string2 => $row['present_odometer'],
                        'Liters' => "<div style='text-align:right'>".$row['gallons']."</p>",
                        'Price/Liter' => "<div style='text-align:right'>".$pr_li."</p>",
                        'Total' => "<div style='text-align:right'>".$tot_cost."</p>",
                        'Km/Hrs' => "<div style='text-align:right'>".$mil."</p>",
                        'Km/L' => "<div style='text-align:right'>".$kml."</p>"
                        );
         }
        if($_SESSION['access_vehicle']==2)
        {
         $arr[] = array(
                        'Date' => $date,
                        'Driver'=>$driver,
                        'Brand' => $row['brand'],
                        'Type' => $row['type'],
                        //'Engine Type' => $row['engine'],
                        //'Account' => $row['account'],
                        //$string1 => $row['last_odometer'],
                        //$string2 => $row['present_odometer'],
                        'Liters' => "<div style='text-align:right'>".$row['gallons']."</p>",
                        'Price/Liter' => "<div style='text-align:right'>".$pr_li."</p>",
                        'Total' => "<div style='text-align:right'>".$tot_cost."</p>",
                        'Km/Hrs' => "<div style='text-align:right'>".$mil."</p>",
                        'Km/L' => "<div style='text-align:right'>".$kml."</p>",
                        'Tools'=>$tools
                        );
        }


      }
      $total_miles = mysql_query("SELECT SUM(miles) as sum_miles FROM veh_fuel_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_miles = mysql_fetch_array($total_miles);

      $total_gallons = mysql_query("SELECT SUM(gallons) as sum_gallons FROM veh_fuel_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_gallons = mysql_fetch_array($total_gallons);

      $total_cost = mysql_query("SELECT SUM(tot_cost) as sum_cost FROM veh_fuel_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_cost = mysql_fetch_array($total_cost);

      $tot2 = $row_cost['sum_cost'];
      $tot_c = $this->commafy($tot2);

      $arr[] = array(
                        'Date' => TOTAL,
                        'Mileage' => "<div style='text-align:right'>".$row_miles['sum_miles']."</p>",
                        'Liters' => "<div style='text-align:right'>".$row_gallons['sum_gallons']."</p>",
                        'Total' => "<div style='text-align:right'>".$tot_c."</p>"
                        );



          $this->JTPlatinumGrid1->setCellData($arr);
       }

}

global $application;

global $Veh_Fuel_List;

//Creates the form
$Veh_Fuel_List=new Veh_Fuel_List($application);

//Read from resource file
$Veh_Fuel_List->loadResource(__FILE__);

//Shows the form
$Veh_Fuel_List->show();

?>